def solution(m, n, array):
    # 目标性格密码是输入数组的第一个元素
    target_code = array[0]
    # 初始化最小距离为正无穷大
    min_distance = float('inf')
    # 初始化最佳性格密码列表为空
    best_codes = []
    # 定义不相容性格对列表
    incompatible_pairs = [('A', 'E'), ('E', 'A'), ('B', 'D'), ('D', 'B'), ('C', 'E'), ('E', 'C'), ('B', 'E'),
                          ('E', 'B')]
    # 遍历除目标性格密码外的每个特种兵性格密码
    for code in array[1:]:
        # 初始化当前距离为 0
        distance = 0
        # 遍历每个维度的字符
        for char_a, char_b in zip(target_code, code):
            # 如果当前维度的字符对是不相容性格对
            if (char_a, char_b) in incompatible_pairs:
                # 将距离设为正无穷大并跳出当前循环
                distance = float('inf')
                break
            else:
                # 获取当前维度目标字符在 ABCDE 中的索引
                index_a = 'ABCDE'.index(char_a)
                # 获取当前维度特种兵字符在 ABCDE 中的索引
                index_b = 'ABCDE'.index(char_b)
                # 计算当前维度的字符差异并累加到总距离
                distance += abs(index_a - index_b)
        # 如果当前距离小于最小距离
        if distance < min_distance:
            # 更新最小距离为当前距离
            min_distance = distance
            # 将最佳性格密码列表重置为当前特种兵性格密码
            best_codes = [code]
        # 如果当前距离等于最小距离
        elif distance == min_distance:
            # 将当前特种兵性格密码添加到最佳性格密码列表中
            best_codes.append(code)
    # 如果最佳性格密码列表不为空，则返回该列表；否则，返回 None
    if best_codes is None:
        return None
    else:
        for code in best_codes:
            return ("".join(code))


if __name__ == "__main__":
    matrix = [
        ["A", "B", "C", "D", "E", "A"],
        ["A", "A", "A", "A", "A", "A"],
        ["B", "B", "B", "B", "B", "B"],
        ["A", "B", "D", "D", "E", "B"],
    ]
    print(solution(6, 3, matrix) == "ABDDEB")
